/*
 * @Author: GL
 * @Date: 2023-04-09 00:35:10
 * @LastEditors: GL
 * @LastEditTime: 2023-04-26 02:26:28
 * @Description: desc
 */
// 导入数据库操作模块
const db = require("../db/index")

// 查询个人订单列表
const lsql = `SELECT * FROM sys_order c
INNER JOIN product p on c.product_id = p.product_id
where p.is_delete=0 and c.is_del=0 AND c.user_id=? AND c.status=?
order by c.order_time desc`
exports.orderList = (req, res) => {
  db.query(lsql, [req.auth.id, req.body.status], (err, results) => {
    // 1. 执行 SQL 语句失败
    if (err) return res.cc(err)

    // 2. 执行 SQL 语句成功
    res.send({
      status: 0,
      message: '获取成功！',
      data: results,
    })
  })
}

// 订单添加
const selectSql = "select * from sys_order where user_id=? and product_id=? and is_del=0"
exports.addOrder = (req, res) => {
  db.query(selectSql, [req.auth.id, req.body.product_id], (err, results) => {

    // 执行 SQL 语句失败
    if (err) return res.cc(err)

    if (results.length === 1 && results[0].product_id == req.body.product_id) {
      return res.send({
        status: 1,
        message: '已有相同订单！',
      })
    }

    const csql = `insert into sys_order set product_id=?,user_id=?,contact_name=?,order_time=?,phone=?
    ,address=?,address_detail=?`
    // 获取当前时间
    const time = new Date()
    db.query(csql, [req.body.product_id, req.auth.id, req.body.contact_name, time, req.body.phone, req.body.address, req.body.address_detail], (err, results) => {
      // SQL 语句执行失败
      if (err) return res.cc(err)

      // SQL 语句执行成功，但是影响行数不等于 1
      if (results.affectedRows !== 1) return res.cc('支付失败！')

      // 支付成功
      res.send({
        status: 0,
        message: '支付成功！',
      })
    })
  })
}

// 订单状态更新
const rsql = `update sys_order set status=? where user_id = ? and id=?`
exports.removeOrder = (req, res) => {
  // res.send('ll')
  db.query(rsql, [req.body.status, req.auth.id, req.body.id], (err, results) => {
    // SQL 语句执行失败
    if (err) return res.cc(err)

    // SQL 语句执行成功，但是影响行数不等于 1
    if (results.affectedRows !== 1) return res.send({
      status: 1,
      message: '状态更新失败！',
    })
    // 移除成功
    res.send({
      status: 0,
      message: '状态更新成功！',
    })
  })
}
// 订单删除
const dsql = `update sys_order set is_del=? where user_id = ? and id=?`
exports.delOrder = (req, res) => {
  db.query(dsql, [req.body.del, req.auth.id, req.body.id], (err, results) => {
    // SQL 语句执行失败
    if (err) return res.cc(err)

    // SQL 语句执行成功，但是影响行数不等于 1
    if (results.affectedRows !== 1) return res.send({
      status: 1,
      message: '删除失败！',
    })
    // 移除成功
    res.send({
      status: 0,
      message: '删除成功！',
    })
  })
}

/**
 * 以上为用户订单接口，以下为商品所有者的订单管理借接口
 */
const adsql = `SELECT * FROM sys_order c
INNER JOIN product p on c.user_id = p.create_by AND c.product_id = p.product_id
where p.is_delete=0 and c.is_del=0 AND c.status=? AND c.user_id = ?
order by c.order_time desc`
exports.userOrderList = (req, res) => {
  db.query(adsql, [req.body.status,req.auth.id], (err, results) => {
    // 1. 执行 SQL 语句失败
    if (err) return res.cc(err)

    // 2. 执行 SQL 语句成功
    res.send({
      status: 0,
      message: '获取成功！',
      data: results,
    })
  })
}
